package usercenter

import (
	"context"
	"database/sql"
	"fmt"
	"github.com/zeromicro/go-zero/core/stores/cache"
	"github.com/zeromicro/go-zero/core/stores/sqlx"
)

var _ UserProfileModel = (*customUserProfileModel)(nil)

type (
	// UserProfileModel is an interface to be customized, add more methods here,
	// and implement the added methods in customUserProfileModel.
	UserProfileModel interface {
		userProfileModel
		InsertTrans(ctx context.Context, tx sqlx.Session, data *UserProfile) (sql.Result, error)
	}

	customUserProfileModel struct {
		*defaultUserProfileModel
	}
)

// NewUserProfileModel returns a model for the database table.
func NewUserProfileModel(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) UserProfileModel {
	return &customUserProfileModel{
		defaultUserProfileModel: newUserProfileModel(conn, c, opts...),
	}
}

func (m *customUserProfileModel) InsertTrans(ctx context.Context, tx sqlx.Session, data *UserProfile) (sql.Result, error) {
	mumbaiUserProfileUidKey := fmt.Sprintf("%s%v", cacheMumbaiUsercenterUserProfileUidPrefix, data.Uid)
	ret, err := m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
		query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?, ?, ?, ?)", m.table, userProfileRowsExpectAutoSet)
		return tx.ExecCtx(ctx, query, data.Uid, data.AreaNo, data.PhoneNo, data.Name, data.Avatar, data.Email, data.Language)
	}, mumbaiUserProfileUidKey)
	return ret, err
}
